package com.enfi.api.domain.res;

import com.google.common.collect.Maps;
import lombok.Builder;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Optional;

/**
 * @author xukk
 * @date 2018/5/4
 */
@Builder
public class ApiResult extends LinkedHashMap<String, Object> {
    private static final long serialVersionUID = 1L;

    /**
     * 初始化一个新创建的 Message 对象
     */
    public ApiResult() {
    }

    public ApiResult success(String... message) {
        init(ApiStatus.SUCCESS.code(), (message == null || message.length == 0) ? ApiStatus.SUCCESS.desc() : message[0], "", "");
        return this;
    }

    public ApiResult failure(String... message) {
        init(ApiStatus.FAIL.code(), ApiStatus.FAIL.desc(), ApiStatus.FAIL.name(), (message == null || message.length == 0) ? ApiStatus.FAIL.desc() : message[0]);
        return this;
    }

    public ApiResult failure(String error, String message) {
        init(ApiStatus.FAIL.code(), ApiStatus.FAIL.desc(), error, message);
        return this;
    }

    public ApiResult failure(Integer status, String message) {
        init(status, ApiStatus.FAIL.desc(), "", message);
        return this;
    }

    public ApiResult failure(Integer status, String error, String message) {
        init(status, ApiStatus.FAIL.desc(), error, message);
        return this;
    }

    public boolean check() {
        return get("status") != null && String.valueOf(get("status")).equals(String.valueOf(ApiStatus.SUCCESS.code()));
    }

    private void init(Integer status, String message, String error, String errorDescription) {
        super.put("status", status);
        super.put("message", message);
        super.put("error", error);
        super.put("error_description", errorDescription);
        super.put("timestamp", System.currentTimeMillis());
    }

    /**
     * 返回成功内容
     *
     * @param key   键值
     * @param value 内容
     * @return 成功消息
     */
    @Override
    public ApiResult put(String key, Object value) {
        if (Optional.ofNullable(get("data")).isPresent()) {
            if (get("data") instanceof Map) {
                ((Map) get("data")).put(key, value);
            }
        } else {
            Map<String, Object> map = Maps.newHashMap();
            map.put(key, value);
            super.put("data", map);
        }
        return this;
    }

    public ApiResult add(Object value) {
        super.put("data", value);
        return this;
    }

    public Object acquire(String key) {
        if (get("data") != null && get("data") instanceof Map) {
            return ((Map) get("data")).get(key);
        } else {
            return null;
        }
    }

    /**
     * 请求路径
     *
     * @param path
     * @return
     */
    public ApiResult path(String path) {
        super.put("path", path);
        return this;
    }

    public String getMessage() {
        return String.valueOf(get("message"));
    }

    public String getError() {
        return String.valueOf(get("error"));
    }

    public String getErrorDescription() {
        return String.valueOf(get("error_description"));
    }

    public Object getData() {
        return get("data");
    }

    public static ApiResult custom() {
        return new ApiResult();
    }


    public static ApiResult custom(boolean flag) {
        if (flag) {
            return new ApiResult().success();
        } else {
            return new ApiResult().failure();
        }

    }

    public static ApiResult custom(boolean flag, String success, String failure) {
        if (flag) {
            return new ApiResult().success(success);
        } else {
            return new ApiResult().failure(failure);
        }
    }


    public static ApiResult renderError(String msg) {
        return ApiResult.custom().failure(msg);
    }
    public static ApiResult renderOk(String msg) {
        return ApiResult.custom().success(msg);
    }

    public static ApiResult renderOk() {
        return ApiResult.custom().success("");
    }
}